package com.zzedu.quartz.config;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.quartz.domain.SysJob;
import com.zzedu.quartz.mapper.SysJobMapper;
import com.zzedu.quartz.util.ScheduleUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *启动后 系统数据加载器
 */
@Component
@Order(Integer.MAX_VALUE)
public class JobLoadRunner implements ApplicationRunner{
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SysJobMapper jobMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        scheduler.clear();
        List<SysJob> jobList = jobMapper.selectList(Wrappers.lambdaQuery(SysJob.class));
        for (SysJob job : jobList)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
    }
}
